package cn.wolfcode.shop.cloud.mapper;

import cn.wolfcode.shop.cloud.domain.Good;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.SelectProvider;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Set;

/**
 * Created by wolfcode-lanxw
 */
@Mapper
public interface GoodsMapper {

    @SelectProvider(type = GoodsSelectProvider.class, method = "selectByIdList")
    List<Good> selectByIdList(@Param("idList") Set<Long> idList);

    class GoodsSelectProvider {

        /**
         * 查询商品列表的提供者
         * @param idList
         * @return 拼接好的 SQL
         */
        public String selectByIdList(@Param("idList") Set<Long> idList) {
            StringBuilder sb = new StringBuilder(100);
            sb.append("select * from t_goods ");
            if (CollectionUtils.isEmpty(idList)) {
                return sb.toString();
            }

            sb.append("where id in (");
            // select * from t_goods where id in (
            for (Long id : idList) {
                sb.append(id).append(",");
            }
            // select * from t_goods where id in (1,2,3,
            sb.deleteCharAt(sb.length()-1);
            // select * from t_goods where id in (1,2,3
            sb.append(")");
            // select * from t_goods where id in (1,2,3)
            return sb.toString();
        }
    }
}
